StartScene = class("StartScene", baseScene)


function StartScene.create()
    local scene = StartScene.new()
    scene:init()
	return scene
end

function StartScene:destroy()

end

function StartScene:init()
    --load res
    gameUtil.playMusic(music.level, true)

    self.view = cc.Scene:create()
    
    self.widget = ccs.GUIReader:getInstance():widgetFromJsonFile("startScene.json")
    self.view:addChild(self.widget) 
    
    if DEF_CHN_XNY 
        and cc.PLATFORM_OS_ANDROID == g_targetPlatform 
        then
        local cs = require "purchase/channelSpecial"
        local strUserId = cs.XNY.strUserId
        if not strUserId then
            strUserId = "strUserId == nil"
        end
        local label = cc.Label:createWithSystemFont(strUserId, pp.FontTTF, 18)
        if label then
            label:setColor(cc.c3b(40,150,255))
            self.widget:addChild(label, 10)
            label:setPosition( cc.p(pp.visibleSize.width*0.82, pp.visibleSize.height*0.02) )
        else
            assert(false, "StartScene: init createTTF error")
        end
    end 

    --mask
    do --mask
        self.base_mask = self.widget:getChildByName("_mask")
        self.base_mask:setVisible(false)
    end
    
    --start
    local function btStartCallback(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn1)
        elseif eventType == ccui.TouchEventType.ended then
            gameUtil.changeScene("level")
           
        end
    end
    self.bt_start = self.widget:getChildByName("_start")
    self.bt_start:addTouchEventListener(btStartCallback)

    uiEffect.elastic(self.bt_start)

    --more game
    local function btMoreCallback(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn1)
        elseif eventType == ccui.TouchEventType.ended then
            --
           
            if cc.PLATFORM_OS_ANDROID == g_targetPlatform and DEF_CHN_CMjd then
                local cs = require "purchase/channelSpecial"
                cs.CMjd:moreGame()
            end

        end
    end
    self.bt_moregame = self.widget:getChildByName("_more_game")
    self.bt_moregame:addTouchEventListener(btMoreCallback)

    if DEF_CHN_CMjd then
        self.bt_moregame:setVisible(true)
    else 
        self.bt_moregame:setVisible(false)
    end




    local selfclass = _G["StartScene"]
    --key
    local function onKeyReleased(keyCode, event)
        local view = event:getCurrentTarget()
        if keyCode == cc.KeyCode.KEY_ESCAPE then
            AudioEngine.playEffect(sound.btn1)

            if cc.PLATFORM_OS_ANDROID == g_targetPlatform and DEF_CHN_CMjd then
                local cs = require "purchase/channelSpecial"
                cs.CMjd:exitGame()
            else

                local win = self:base_isWinOpened("WarningPanel")
                if not win then
                    local win = WarningPanel.create(self, "Common_leaveApp", pp.z_leaveApp)
                else
                    win:close()
                end

            end

        end
    end
    local listener = cc.EventListenerKeyboard:create()
    listener:registerScriptHandler(onKeyReleased, cc.Handler.EVENT_KEYBOARD_RELEASED )
    local eventDispatcher = self.view:getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self.view)
end

function StartScene:warningPanelCallback(type, btIdx, panel)
    if type == "Common_leaveApp" then
        if btIdx == 0 then
        elseif btIdx == 2 then
            panel:close()
        elseif btIdx == 1 then
            cc.Director:getInstance():endToLua()
        else
            
        end
    end
end